package com.example.opengldemo._112_;

import android.content.Context;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;

import com.example.opengldemo.R;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.TextureUtil;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MyMountionView extends BaseView  {

    Mountion mountion ;
    public MyMountionView(Context context) {
        super(context);
    }
    int desertTextureId ;
    int greenTextureId ;

    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        GLES30.glClearColor(0.3f , 0.3f ,0.3f , 1.0f);
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
        GLES30.glEnable(GLES30.GL_CULL_FACE);
        MatrixState.setInitStack();
        mountion = new Mountion(this) ;
        desertTextureId = TextureUtil.getTextureId(this , R.mipmap.grass , null, false) ;
        greenTextureId = TextureUtil.getTextureId(this , R.mipmap.rock , null, false) ;
    }

    @Override
    public void onSurfaceChanged(GL10 gl10, int i, int i1) {
        GLES30.glViewport(0 , 0 , i , i1);
        float des = (float)i/i1 ;
        MatrixState.setProjectFrustum(-des , des  ,-1 , 1 , 5 , 1000 );
        MatrixState.setCamera( 0 , 0 , 7 , 0 , 0  , 0 , 0 ,1, 0);
    }

    @Override
    public void onDrawFrame(GL10 gl10) {
        GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_COLOR_BUFFER_BIT);
        MatrixState.pushMatrix();
        MatrixState.setCamera(
                MatrixState.cameraLocation[0],
                MatrixState.cameraLocation[1],
                MatrixState.cameraLocation[2],
                0,
                0,
                0,
                0,
                1,
                0
        );
        mountion.drawSelf(desertTextureId , greenTextureId);
        MatrixState.popMatrix();
    }
}
